﻿using DataCenter.Entitys;
using DataCenter.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.MemoryRepository
{
    public class ProductRepository : IProductRepository
    {
        private static List<Product> _products = new List<Product>()
        {
            new Product(){ Id=Guid.Parse("38b69d4d-a5e5-cc50-65a1-2ba426e4a641"), Name="乔峰", CustomerId=Guid.Parse("70c92219-a484-1614-76ad-bdc2dbacf9af")},
            new Product(){ Id=Guid.Parse("b30cdbd7-1543-15b4-ffe9-ac96b815b1e3"), Name="段誉", CustomerId=Guid.Parse("70c92219-a484-1614-76ad-bdc2dbacf9af")},
            new Product(){ Id=Guid.Parse("e5d1d9a3-bdc4-5b1e-afdb-9b6b73bd93cb"), Name="郭靖",CustomerId=Guid.Parse("ccb12173-10a9-8073-6c09-a478cb604177")},
            new Product(){ Id=Guid.Parse("814d9de0-86b6-6ffe-6007-67d60d61e2e6"), Name="杨过", CustomerId=Guid.Parse("50df1479-8f64-e96a-8597-3cf4ab5e4fd1")}

        };
        public async Task<int> Add(Product product)
        {
            _products.Add(product);
            return await Task.FromResult(1);
        }

        public async Task<int> DeleteById(Guid id)
        {
            var delItme = _products.FirstOrDefault(item => item.Id == id);
            _products.Remove(delItme);
            return await Task.FromResult(1);
        }

        public async Task<IEnumerable<Product>> GetAll()
        {
            return await Task.FromResult(_products);
        }

        public  async Task<Product> GetById(Guid id)
        {
            return await Task.FromResult(_products.FirstOrDefault(item => item.Id == id));
        }

        public async Task<int> Update(Product product)
        {
            var delItme = _products.FirstOrDefault(item => item.Id == product.Id);
            _products.Remove(delItme);
            _products.Add(product);
            return await Task.FromResult(1);
        }
    }
}
